﻿package 
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import Player;
	public class DRGame extends MovieClip
	{
		private const UP:Number = 87;
		private const DOWN:Number = 83;
		private const LEFT:Number = 65;
		private const RIGHT:Number = 68;
		
		
		public var player:MovieClip;
		private var ItemGUI:MovieClip = new GUI_Items();
		private var GUI_offset:Number;
		public function DRGame():void
		{
			player = new Player();
			stage.addChild(ItemGUI)
			stage.addChild(player);
			ItemGUI.x = stage.stageWidth / 2;
			ItemGUI.y = stage.stageHeight - (ItemGUI.height / 2);
			player.x = stage.stageWidth / 2;
			player.y = stage.stageHeight / 2;
			GUI_offset = Math.abs(stageFrame.y - ItemGUI.y);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keydownHandler);
			stage.addEventListener(KeyboardEvent.KEY_UP, keyupHandler);
			stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
			stage.addEventListener(Event.ENTER_FRAME, Update);
			
		}
		
		private function mouseUpHandler(e:MouseEvent):void 
		{
			if (e.target == "[object Item1]" || e.target == "[object Item2]")
			{
				e.target.stopDrag();
			}
		}
		
		private function mouseDownHandler(e:MouseEvent):void 
		{
			//trace(e.target);
			var tempObject:Object = e.target;
			if (tempObject == "[object Item1]" && tempObject.getIsRoot())
			{
				var newObject:MovieClip = new Item1(false);
				stage.addChild(newObject);
				var Item1Point1:Point = new Point(tempObject.x, tempObject.y);
				var Item1Point2:Point = ItemGUI.localToGlobal(Item1Point1);
				newObject.x = Item1Point2.x;
				newObject.y = Item1Point2.y;
				newObject.startDrag();
			}
			if (tempObject == "[object Item2]" && tempObject.getIsRoot())
			{
				var newObject2:MovieClip = new Item2(false);
				stage.addChild(newObject2);
				var Item2Point1:Point = new Point(tempObject.x, tempObject.y);
				var Item2Point2:Point = ItemGUI.localToGlobal(Item2Point1);
				newObject2.x = Item2Point2.x;
				newObject2.y = Item2Point2.y;
				newObject2.startDrag();
			}
		}
		private function keydownHandler(evt:KeyboardEvent):void
		{
			switch(evt.keyCode)
			{
				case UP:
				player.setUpDown(true);
				break;
				case LEFT:
				player.setLeftDown(true);
				break;
				case DOWN:
				player.setDownDown(true);
				break;
				case RIGHT:
				player.setRightDown(true);
				break;
			}
		}
		private function keyupHandler(evt:KeyboardEvent):void
		{
			switch(evt.keyCode)
			{
				case UP:
				player.setUpDown(false);
				break;
				case LEFT:
				player.setLeftDown(false);
				break;
				case DOWN:
				player.setDownDown(false);
				break;
				case RIGHT:
				player.setRightDown(false);
				break;
			}
		}
		
		private function Update(evt:Event):void
		{
			player.Update();
			MoveCamera();
		}
		
		private function MoveCamera():void
		{
			camera.x = player.x;
			camera.y = player.y;
			stageFrame.x = player.x;
			stageFrame.y = player.y;
			ItemGUI.x = player.x;
			ItemGUI.y = stageFrame.y + GUI_offset;
		}
	}
}